package sqlexplorer.domain;

import java.sql.Connection;
import java.sql.DriverManager;

import sqlexplorer.domain.exceptions.ConnectionFailedException;

public class DatabaseConnection {
	
	private String m_url;
	private String m_name;
	private String m_userName;
	private String m_password;
	private DatabaseType m_type;
	
	public DatabaseConnection(String url,String name,String userName,String password,DatabaseType type){
		m_url=url;
		m_name=name;
		m_userName=userName;
		m_password=password;
		m_type=type;
	}
	
	public Connection connect() throws ConnectionFailedException{
		try{
			Class.forName(DatabaseType.getDriverName(m_type));
			Connection connection=DriverManager.getConnection(m_url,m_userName,m_password);
			
			return connection;
		}catch(Exception exc){
			ConnectionFailedException cfe=new ConnectionFailedException(exc.getMessage());
			cfe.setStackTrace(exc.getStackTrace());
			
			throw cfe;
		}
	}
	
	public String getJdbcUrl(){
		return m_url;
	}
	
	public void setJdbcUrl(String url){
		m_url=url;
	}
	
	public String getConnectionName(){
		return m_name;
	}
	
	public void setConnectionName(String name){
		m_name=name;
	}
	
	public String getUserName(){
		return m_userName;
	}
	
	public void setUserName(String userName){
		m_userName=userName;
	}
	
	public String getPassword(){
		return m_password;
	}
	
	public void setPassword(String password){
		m_password=password;
	}
	
	public DatabaseType getDatabaseType(){
		return m_type;
	}
	
	public void setDatabaseType(DatabaseType type){
		m_type=type;
	}
}
